<template>
    <div>
        <el-form :inline="true" class="demo-form-inline" >
            <el-form-item>
                <!--输入框-->
                <el-input
                        v-model="searchForm.name"
                        placeholder="名称"
                        clearable
                ></el-input>
            </el-form-item>
            <el-form-item>
                <!--搜索按钮-->
                <el-button @click="getRoleList">搜索</el-button>
            </el-form-item>
            <el-form-item>
                <!--新增按钮-->
                <el-button type="primary" @click="dialogFormVisible = true">新增</el-button>
            </el-form-item>
            <el-form-item>
                <!--批量删除按钮-->
                <el-popconfirm
                        title="确定批量删除选中的内容吗？"
                        @confirm="handleDelete">
                    <el-button  type="danger" slot="reference" :disabled="!this.multipleSelection.length">批量删除</el-button>
                </el-popconfirm>
            </el-form-item>
        </el-form>
        <el-table
                ref="multipleTable"
                :data="roleList"
                tooltip-effect="dark"
                max-height="578"
                style="width: 100%"
                border
                stripe
                @selection-change="handleSelectionChange">
            <el-table-column
                    type="selection"
                    width="55"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="name"
                    label="名称"
                    width="120">
            </el-table-column>
            <el-table-column
                    prop="code"
                    label="唯一编码"
                    show-overflow-tooltip>
            </el-table-column>
            <el-table-column
                    prop="remark"
                    label="描述"
                    show-overflow-tooltip>
            </el-table-column>
            <el-table-column
                    prop="status"
                    label="状态"
                    width="auto">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.status" type="success" size="small">正常</el-tag>
                    <el-tag v-else type="danger" size="small">禁用</el-tag>
                </template>
            </el-table-column>
            <el-table-column
                    prop="options"
                    label="操作">
                <template slot-scope="scope">
                    <el-button size="mini" @click="handlePerms(scope.row.id)">分配权限</el-button>
                    <el-divider direction="vertical"></el-divider>
                    <el-button type="primary" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
                    <el-divider direction="vertical"></el-divider>
                    <el-popconfirm
                            title="确定删除这一项内容吗？"
                            @confirm="handleDelete(scope.row.id)"
                    >
                        <el-button  size="mini" type="danger" slot="reference">删除</el-button>
                    </el-popconfirm>

                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :total="roleList.length"
                :page-sizes="sizes"
                :page-size="size"
                layout="total, sizes, prev, pager, next, jumper"
        >
        </el-pagination>
        <!--添加和编辑对话框-->
        <el-dialog
                width="600px"
                title="角色管理"
                :visible.sync="dialogFormVisible"
                :before-close="handleEditClose"
        >
            <el-form :model="editForm" :rules="editFormRules" ref="editForm" label-width="100px" class="demo-editForm">
                <el-form-item label="角色名称" prop="name">
                    <el-input v-model="editForm.name"></el-input>
                </el-form-item>
                <el-form-item label="唯一编码" prop="code">
                    <el-input v-model="editForm.code"></el-input>
                </el-form-item>
                <el-form-item label="描述" prop="remark">
                    <el-input v-model="editForm.remark"></el-input>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-radio-group v-model="editForm.status">
                        <el-radio :label=1>正常</el-radio>
                        <el-radio :label=0>禁用</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="resetForm('editForm')">取 消</el-button>
                <el-button type="primary" @click="submitForm('editForm')">确 定</el-button>
            </div>
        </el-dialog>
        <!--分配角色对话框-->
        <el-dialog
                width="600px"
                title="分配权限"
                :visible.sync="permDialogFormVisible"
        >
            <el-form :model="permForm">
                <el-tree
                        :data="permTreeData"
                        :props="defaultProps"
                        ref="permTree"
                        node-key="id"
                        :check-strictly="checkStrictly"
                        show-checkbox
                        expand-on-click-node
                        default-expand-all
                >
                </el-tree>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="permDialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitPermForm">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: 'Role',
        data() {
            return {
                searchForm: {},

                roleList: [],
                multipleSelection: [],
                currentPage: 0,
                sizes: [],
                size: 0,

                dialogFormVisible: false,
                editForm: {
                    name: '',
                    code: '',
                    remark: '',
                    status: '',
                },
                editFormRules: {
                    name: [
                        { required: true, message: '请输入角色名称', trigger: 'blur' }
                    ],
                    code: [
                        { required: true, message: '请输入唯一编码', trigger: 'blur' }
                    ],
                    status: [
                        { required: true, message: '请选择角色状态', trigger: 'blur' }
                    ],
                },

                permDialogFormVisible: false,
                permTreeData: [],
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                permForm: {},
                // 第一次标记
                first: true,
                // 树形节点子父级是否关联 动态修改子父级是否关联
                checkStrictly: true,
            }
        },
        created() {
            this.getRoleList();
            this.getPermTreeData();
        },
        methods: {
            // 获取角色列表
            getRoleList() {
                this.$axios.get('/sys/role/list', {
                    params: {
                        name: this.searchForm.name,
                        current: this.currentPage,
                        size: this.size
                    }
                }).then(res => {
                    // 角色列表
                    this.roleList = res.data.data.roleList;
                    // 当前页
                    this.currentPage = res.data.data.current;
                    // 第一次访问时
                    if (this.first) {
                        this.size = res.data.data.size;
                        for (let i = 0; i < 4; i++) {
                            this.sizes.push(this.size + 10 * i);
                        }
                        this.first = false;
                    }
                });
            },
            // 获取树形菜单列表
            getPermTreeData() {
                this.$axios.get('/sys/menu/list').then(res => {
                    this.permTreeData = res.data.data;
                });
            },
            // 表格选中框
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 页码变化
            handleSizeChange(val) {
                this.size = val;
                this.getRoleList();
            },
            // 当前页
            handleCurrentChange(val) {
                this.currentPage = val;
                this.getRoleList();
            },
            // 重置表单
            resetForm(formName) {
                this.$refs[formName].resetFields();
                this.editForm = {
                    name: '',
                    code: '',
                    remark: '',
                    status: '',
                };
                this.dialogFormVisible = false;
            },
            handleEditClose() {
                this.resetForm('editForm');
            },
            // 提交表单
            submitForm(formName) {
                this.$refs[formName].validate(valid => {
                    if (valid) {
                        this.$axios.post('/sys/role/'+(this.editForm.id?'update':'save'), this.editForm).then(res => {
                            this.$message({
                                showClose: true,
                                message: '操作成功~',
                                duration: 1000,
                                type: 'success',
                                onClose: () => {
                                    this.getRoleList();
                                }
                            });
                            this.dialogFormVisible = false;
                            this.resetForm(formName);
                        });
                    } else {
                        this.$message.error('操作失败！');
                        return false;
                    }
                });
            },
            // 编辑按钮
            handleEdit(item) {
                /*this.$axios.get('/sys/menu/info/'+i).then(res => {
                    this.editForm = res.data.data;
                    this.dialogFormVisible = true;
                });*/
                // 深拷贝
                this.editForm = JSON.parse(JSON.stringify(item));
                this.dialogFormVisible = true;
            },
            // 删除
            handleDelete(id) {
                let ids = [];
                if (id==undefined) {
                    this.multipleSelection.forEach(v => ids.push(v.id));
                } else {
                    ids.push(id);
                }
                this.$axios.post('/sys/role/delete', ids).then(res => {
                    this.$message({
                        showClose: true,
                        message: '操作成功~',
                        duration: 1000,
                        type: 'success',
                        onClose: () => {
                            this.getRoleList();
                        }
                    });
                });
            },
            // 打开分配权限对话框
            handlePerms(id) {
                // 子父级不关联
                this.checkStrictly = true;
                this.permDialogFormVisible = true;
                this.$axios.get('/sys/role/info' + id).then(res => {
                    this.$refs.permTree.setCheckedKeys(res.data.data.menuIds);
                    this.permForm = res.data.data;
                    // 展示完后子父级关联
                    this.checkStrictly = false;
                });

            },
            // 提交树形表单
            submitPermForm() {
                let checked = this.$refs.permTree.getCheckedKeys();
                this.$axios.post('/sys/role/perm'+this.permForm.id, checked).then(res => {
                    this.$message({
                        showClose: true,
                        message: '操作成功~',
                        duration: 1000,
                        type: 'success',
                        onClose: () => {
                            this.getRoleList();
                        }
                    });
                    this.permDialogFormVisible = false;
                });
            },
        }
    }
</script>

<style scoped>
    .el-pagination{
        float: right;
        margin-top: 15px;
    }
</style>
